//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Text;
using System.Web;

namespace CommunityServer.Components.HttpHandler {

    public class VCardHttpHandler : IHttpHandler {

        public void ProcessRequest (HttpContext context) {

            byte[] vcard;


            try {
                // Get the requested user's vcard
                vcard = BuildVCard(context);

                // Set the response type
                context.Response.ContentType = "text/x-vcard";

                // Output the content
                context.Response.OutputStream.Write(vcard, 0, vcard.Length);
                context.Response.End();
            } catch {
                context.Response.Write("Unable to retrieve vcard");
            }


        }

        public byte[] BuildVCard(HttpContext context) 
        {
            StringBuilder vcard = new StringBuilder();
            User user = Users.GetUser(int.Parse(context.Request.QueryString["UserID"]), false, true);

            vcard.Append("BEGIN:VCARD\nVERSION:2.1\n");

            // NickName
            vcard.Append("NICKNAME:" + user.DisplayName + "\n");

            // URL
            //
            vcard.Append("URL;WORK:" + user.Profile.WebAddress + "\n");

            // Title
            vcard.Append("ROLE:" + user.Profile.Occupation + "\n");

            // Email
            vcard.Append("EMAIL;PREF;INTERNET:" + user.Profile.PublicEmail + "\n");

            // End the VCARD
            vcard.Append("\nREV:20030217T210833Z\nEND:VCARD");

            ASCIIEncoding asciiEncoding = new ASCIIEncoding();

            return asciiEncoding.GetBytes(vcard.ToString());

        }

        public bool IsReusable {
            get {
                return false;
            }
        }

    }
}
